package com.jcfk.system.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jcfk.common.business.enums.AvailableStatus;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.business.utils.DataBindHelper;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.ReqPageParam;
import com.jcfk.common.web.domain.RetPage;
import com.jcfk.common.web.domain.RetResult;

import com.jcfk.system.domain.dto.SysPermissionDTO;
import com.jcfk.system.domain.po.SysBusinessGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import com.jcfk.system.domain.query.SysBusinessGroupQuery;
import com.jcfk.system.domain.dto.SysBusinessGroupDTO;
import com.jcfk.system.service.SysBusinessGroupService;
import com.jcfk.common.core.utils.ExcelUtils;

import static com.jcfk.common.core.utils.BeanUtils.convertList;

/**
 * @author awei
 * @email 522891237@gqq.com
 * @date 2023-12-27
 */
@Api(tags = {"管理"})
@RestController
@RequestMapping("sysbusinessgroup")
public class SysBusinessGroupController {
    @Autowired
    private SysBusinessGroupService sysBusinessGroupService;


    /**
     * 分页列表
     */
    @ApiImplicitParams({@ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<SysBusinessGroupQuery>", name = "requestParam", value = "", required = true)})
    @ApiOperation("分页查询")
    @PostMapping("/queryByPage")
    @ResponseBody
    public RetPage<SysBusinessGroupDTO> queryByPage(@RequestBody ReqPageParam<SysBusinessGroupQuery> requestParam) {
        DataPage<SysBusinessGroupDTO> page = new DataPage<SysBusinessGroupDTO>((long) requestParam.getCurrent(), (long) requestParam.getSize(), requestParam.getSort().getOrderItem(), SysBusinessGroupDTO.class);
        page = sysBusinessGroupService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }


    /**
     * 根据id 查询详情
     */
    @ApiOperation("查询")
    @GetMapping("/get/{id}")
    @ResponseBody
    public RetResult<SysBusinessGroupDTO> get(@PathVariable("id") String id) {
        SysBusinessGroupDTO item = sysBusinessGroupService.get(id);
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理
     */
    @ApiOperation("保存")
    @PostMapping("/save")
    @ResponseBody
    @RequestLogHeader(modular = "管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid SysBusinessGroupDTO sysBusinessGroup) {
        // 数据校验
        RetResult ret = sysBusinessGroupService.isValid(sysBusinessGroup);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        if (StrUtil.isEmpty(sysBusinessGroup.getId())) {
            // 新增
            sysBusinessGroupService.insert(sysBusinessGroup);
        } else {
            // 更新
            sysBusinessGroupService.update(sysBusinessGroup);
        }
        return RetResult.ok();

    }


    /**
     * 批量删除
     */
    @ApiOperation("批量删除")
    @ResponseBody
    @PostMapping("/delete")
    @RequestLogHeader(modular = "管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody SysBusinessGroupDTO sysBusinessGroup) {
        return RetResult.ok(sysBusinessGroupService.logicDeleteList(sysBusinessGroup));
    }


    /**
     * 批量删除
     */
    @ApiOperation("获取列表")
    @ResponseBody
    @PostMapping("/getlist")
    @RequestLogHeader(modular = "业务组别管理", businessType = "获取列表")
    public RetResult<?> getlist(@RequestBody SysBusinessGroupDTO dto) {
        QueryWrapper<SysBusinessGroup> queryWrapper = new QueryWrapper<>();

        if (dto.getIds() != null && dto.getIds().size() > 0) {
            queryWrapper.lambda().eq(SysBusinessGroup::getDeleted, 0).and(wapper -> wapper.eq(SysBusinessGroup::getEnabled, 1).or().in(SysBusinessGroup::getId, dto.getIds()));
        } else {
            queryWrapper.lambda().eq(SysBusinessGroup::getDeleted, 0).eq(SysBusinessGroup::getEnabled, 1);
        }

        return RetResult.ok(convertList(sysBusinessGroupService.list(queryWrapper), SysBusinessGroupDTO.class));
    }
}
